﻿namespace Utility
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.SqlClient;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    public partial class SQL
    {

        /// <summary>
        /// [English]
        /// Method that performs an Transac-SQL query. example: INSERT INTO [dbo] [Visitors] ([Name], [Last Name]) VALUES ('My Name', 'My Name');
        /// [Spanish]
        /// Método que realiza una consulta Transac-SQL. ejemplo: INSERT INTO [dbo] [Visitantes] ([Nombre], [Apellido]) VALUES ('Mi nombre', 'Mi nombre');
        /// </summary>
        /// <param name="strQuery">English, Insert your Transac-SQL query. Spanish, Inserte aquí su consulta Transac-SQL.</param>
        /// <param name="connectionString">English, Insert here your connection string. Spanish, Inserte aquí su cadena de conexion</param>
        public void CmdQuery(string strQuery, string connectionString)
        {
            using (SqlConnection con = new SqlConnection(connectionString))
            {
                using (SqlCommand Command = new SqlCommand(strQuery, con))
                {
                    con.Open();
                    Command.CommandType = CommandType.Text;
                    Command.CommandText = strQuery;
                    Command.Connection = con;
                    Command.ExecuteNonQuery();
                }

            }
        }

        /// <summary>
        /// [English]
        /// Method that performs an Transac-SQL query. example: "SELECT  * FROM Visitantes"
        /// [Spanish]
        /// Método que realiza una consulta Transac-SQL. ejemplo: "SELECT  * FROM Visitantes"
        /// </summary>
        /// <param name="strQuery">English, Insert your Transac-SQL query. Spanish, Inserte aquí su consulta Transac-SQL.</param>
        /// <param name="connectionString">English, Insert here your connection string. Spanish, Inserte aquí su cadena de conexion</param>
        /// <returns>English, The return value is a DataTable. Spanish, El valor de retorno es un DataTable.</returns>
        public DataTable CmdQueryToDataTable(string strQuery, string connectionString)
        {
            DataTable oDataSet = new DataTable();

            using (SqlConnection con = new SqlConnection(connectionString))
            {
                using (SqlDataAdapter adapter = new SqlDataAdapter(strQuery, con))
                {
                    using (new SqlCommandBuilder(adapter))
                    {
                        adapter.Fill(oDataSet);
                        con.Open();
                    }
                }
            }
            return oDataSet;
        }



    }
}
